package bepviet.android.engine;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import bepviet.android.properties.User;

public class WebServiceAdapter {
	private String SOAP_ACTION;
	private String METHOD_NAME;
	private String NAMESPACE = "http://tempuri.org/";
	private String URL = "http://10.0.2.2:52863/WebService1.asmx";
	private SoapObject responseBody = null;

	public WebServiceAdapter(String METHOD_NAME) {
		this.METHOD_NAME = METHOD_NAME;
		this.SOAP_ACTION = "http://tempuri.org/" + METHOD_NAME;
	}

	public SoapObject tableRow() {

		try {
			AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
					URL);

			SoapSerializationEnvelope sse = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);

			sse.dotNet = true;

			SoapObject client = new SoapObject(NAMESPACE, METHOD_NAME);

			sse.setOutputSoapObject(client);

			sse.bodyOut = client;

			androidHttpTransport.call(SOAP_ACTION, sse);

			responseBody = (SoapObject) sse.getResponse();

			SoapObject table = (SoapObject) responseBody.getProperty(1);

			return (SoapObject) table.getProperty(0);

		} catch (Exception ex) {
			return null;
		}

	}

	public String Login(String username, String password) {

		try {
			AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
					URL);

			SoapSerializationEnvelope sse = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);

			sse.dotNet = true;

			SoapObject client = new SoapObject(NAMESPACE, METHOD_NAME);

			sse.setOutputSoapObject(client);

			sse.bodyOut = client;

			client.addProperty("username", username);
			client.addProperty("password", password);

			androidHttpTransport.call(SOAP_ACTION, sse);

			Object object = (Object) sse.getResponse();

			return object.toString();

		} catch (Exception ex) {
			Log.v("tag", ex.getMessage());
			return "";
		}

	}

	public String Rating(String username, int MaMonAn, float rating) {

		try {
			AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
					URL);

			SoapSerializationEnvelope sse = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);

			sse.dotNet = true;

			SoapObject client = new SoapObject(NAMESPACE, METHOD_NAME);

			sse.setOutputSoapObject(client);

			sse.bodyOut = client;

			client.addProperty("user_id", username);
			client.addProperty("MaMonAn", String.valueOf(MaMonAn));
			client.addProperty("rating",  String.valueOf(rating));
			
			androidHttpTransport.call(SOAP_ACTION, sse);

			Object object = (Object) sse.getResponse();

			return object.toString();

		} catch (Exception ex) {
			Log.v("tag", ex.getMessage());
			return "";
		}

	}
}
